//
//  HLMeViewController.m
//  zhuanxian
//
//  Created by zhaoliang chen on 2017/11/16.
//  Copyright © 2017年 huiling. All rights reserved.
//

#import "HLMeViewController.h"
#import "HLChangePasswordVC.h"
#import "HLFeedbackVC.h"
#import "HLAboutVC.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

@interface HLMeViewController ()
<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView* tableViewMe;

@end

@implementation HLMeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.fd_prefersNavigationBarHidden = YES;
    
    [self.view addSubview:self.tableViewMe];
    [self.tableViewMe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    UIImageView* headerView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"hl_me_header"]];
    headerView.height = 180;
    self.tableViewMe.tableHeaderView = headerView;
    if (@available(iOS 11.0, *)) {
        self.tableViewMe.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
        case 2: {
            return 1;
        }
            break;
        case 1: {
            return 4;
        }
            break;
        default: {
            return 0;
        }
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1 && indexPath.row == 0) {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"MeCell1"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MeCell1"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            UISwitch* switchNotifies = [[UISwitch alloc]initWithFrame:CGRectZero];
            switchNotifies.on = NO;
            [cell.contentView addSubview:switchNotifies];
            [switchNotifies mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.mas_equalTo(cell.contentView);
                make.right.mas_equalTo(-10);
            }];
        }
        cell.textLabel.text = @"消息通知";
        return cell;
    } else if (indexPath.section == 1 && indexPath.row == 3) {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"MeCell2"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MeCell2"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = @"当前版本";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"(当前版本:V%@)",getAppVersion()];
        return cell;
    } else {
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"MeCell3"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MeCell3"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        switch (indexPath.section) {
            case 0: {
                cell.textLabel.text = @"修改密码";
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.textLabel.textAlignment=NSTextAlignmentLeft;
            }
                break;
            case 1: {
                switch (indexPath.row) {
                    case 1: {
                        cell.textLabel.text = @"意见反馈";
                        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                        cell.textLabel.textAlignment=NSTextAlignmentLeft;
                    }
                        break;
                    case 2: {
                        cell.textLabel.text = @"关于我们";
                        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                        cell.textLabel.textAlignment=NSTextAlignmentLeft;
                    }
                        break;
                    default:
                        break;
                }
            }
                break;
            case 2: {
                cell.textLabel.text = @"退出登录";
                cell.textLabel.textAlignment=NSTextAlignmentCenter;
            }
                break;
            default:
                break;
        }
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0: {
            HLChangePasswordVC* vc = [[HLChangePasswordVC alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1: {
            switch (indexPath.row) {
                case 0: {
                    
                }
                    break;
                case 1: {
                    HLFeedbackVC* vc = [[HLFeedbackVC alloc]init];
                    vc.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                case 2: {
                    HLAboutVC* vc = [[HLAboutVC alloc]init];
                    vc.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 2: {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"loginoutNotification" object:nil];
        }
            break;
        default:
            break;
    }
}

- (UITableView*)tableViewMe {
    if (!_tableViewMe) {
        _tableViewMe = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableViewMe.dataSource = self;
        _tableViewMe.delegate = self;
    }
    return _tableViewMe;
}

@end
